package com.dayu.recovery.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.dayu.common.utils.DateUtils;
import com.dayu.common.utils.SecurityUtils;
import com.dayu.recovery.domain.RecoveryGoods;
import com.dayu.recovery.domain.constant.GoodsConstant;
import com.dayu.recovery.mapper.RecoveryGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dayu.recovery.mapper.RecoveryLoanMapper;
import com.dayu.recovery.domain.RecoveryLoan;
import com.dayu.recovery.service.IRecoveryLoanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 暂借登记Service业务层处理
 *
 * @author HK
 * @date 2021-04-16
 */
@Service
public class RecoveryLoanServiceImpl implements IRecoveryLoanService
{
    @Autowired
    private RecoveryLoanMapper recoveryLoanMapper;
    @Autowired
    private RecoveryGoodsMapper goodsMapper;
    /**
     * 查询暂借登记
     *
     * @param loanId 暂借登记ID
     * @return 暂借登记
     */
    @Override
    public RecoveryLoan selectRecoveryLoanById(Long loanId)
    {
        return recoveryLoanMapper.selectRecoveryLoanById(loanId);
    }

    /**
     * 查询暂借登记列表
     *
     * @param recoveryLoan 暂借登记
     * @return 暂借登记
     */
    @Override
    public List<RecoveryLoan> selectRecoveryLoanList(RecoveryLoan recoveryLoan)
    {
        return recoveryLoanMapper.selectRecoveryLoanList(recoveryLoan);
    }

    /**
     * 新增暂借登记
     *
     * @param recoveryLoan 暂借登记
     * @return 结果
     */
    @Override
    public int insertRecoveryLoan(RecoveryLoan recoveryLoan)
    {
        recoveryLoan.setCreateTime(DateUtils.getNowDate());
        return recoveryLoanMapper.insertRecoveryLoan(recoveryLoan);
    }

    /**
     * 修改暂借登记
     *
     * @param recoveryLoan 暂借登记
     * @return 结果
     */
    @Override
    @Transactional
    public int updateRecoveryLoan(RecoveryLoan recoveryLoan)
    {
        //结束事件
        if(recoveryLoan.getStatus() != null && recoveryLoan.getStatus() == 2) {
            RecoveryGoods goods = new RecoveryGoods();
            goods.setGoodsStatus(GoodsConstant.GOODS_STATUS_OK);
            goods.setGoodsId(recoveryLoan.getGoodsId());
            goodsMapper.updateRecoveryGoods(goods);
        }
        return recoveryLoanMapper.updateRecoveryLoan(recoveryLoan);
    }

    /**
     * 批量删除暂借登记
     *
     * @param loanIds 需要删除的暂借登记ID
     * @return 结果
     */
    @Override
    public int deleteRecoveryLoanByIds(Long[] loanIds)
    {
        return recoveryLoanMapper.deleteRecoveryLoanByIds(loanIds);
    }

    /**
     * 删除暂借登记信息
     *
     * @param loanId 暂借登记ID
     * @return 结果
     */
    @Override
    public int deleteRecoveryLoanById(Long loanId)
    {
        return recoveryLoanMapper.deleteRecoveryLoanById(loanId);
    }

    /**
     * 批量新增
     * @param list 新增列表
     * @return 结果
     */
    @Override
    @Transactional
    public int addLoanList(List<RecoveryLoan> list) {
        List<RecoveryGoods> goodsList = new ArrayList<>();
        for(RecoveryLoan loan : list) {
            loan.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
            loan.setCreateTime(new Date());
            RecoveryGoods goods = new RecoveryGoods();
            goods.setGoodsId(loan.getGoodsId());
            goods.setGoodsStatus(GoodsConstant.GOODS_STATUS_LOAN);
            goodsList.add(goods);
        }
        goodsMapper.updateGoodsStatusByList(goodsList);
        return recoveryLoanMapper.insertRecoveryLoanByList(list);
    }
}
